﻿// --------------------------------
// <copyright file="MainForm.cs" company="Pattern Web Solutions">
//     Copyright © Simon Gill 2010. Distributed under the GPL.
// </copyright>
// <author>Simon Gill</author>
// ---------------------------------
// This file is part of League Manager

// League Manager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.

// League Manager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with League Manager.  If not, see <http://www.gnu.org/licenses/>.

namespace LeagueManager
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using LeagueManager.Models;

    public partial class MainForm : Form
    {
        private IMembershipRepository membership;

        public MainForm(IMembershipRepository repository)
        {
            this.InitializeComponent();

            this.membership = repository;

            this.LoadMembers();
            this.LoadGames();

            this.LoadResults();

            this.SetDefaultGame();
        }

        private void LoadResults()
        {
            var results = this.membership.AllGameResults().ToList();

            this.gvResults.DataSource = results;

            for (int i = 0; i < this.gvResults.ColumnCount; i++)
            {
                this.gvResults.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.DisplayedCells);
            }

            this.gvResults.Columns["ID"].Visible = false;
        }

        private void SetDefaultGame()
        {
            this.cmbWinner.Text = string.Empty;
            this.cmbLoser.Text = string.Empty;

            this.chkTie.Checked = false;
        }

        private void LoadMembers()
        {
            Member[] members = this.membership.AllMembers().ToArray();

            this.cmbWinner.Items.Clear();
            this.cmbWinner.Items.AddRange(members);

            this.cmbLoser.Items.Clear();
            this.cmbLoser.Items.AddRange(members);
        }

        private void LoadGames()
        {
            Game[] games = this.membership.AllActiveGames().ToArray();
            this.cmbGame.Items.Clear();
            this.cmbGame.Items.AddRange(games);
        }

        private void EditGames_Click(object sender, EventArgs e)
        {
            GameList gl = new GameList(this.membership);
            gl.ShowDialog();
            this.LoadGames();
        }

        private void EditMember_Click(object sender, EventArgs e)
        {
            MemberViewer mv = new MemberViewer(this.membership);
            mv.ShowDialog();
            this.LoadMembers();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (this.cmbGame.SelectedItem != null &&
                this.cmbLoser.SelectedItem != null &&
                this.cmbWinner.SelectedItem != null)
            {
                GameResult newResult = new GameResult();

                newResult.GameID = (this.cmbGame.SelectedItem as Game).ID;
                newResult.Loser = (this.cmbLoser.SelectedItem as Member).ID;
                newResult.Winner = (this.cmbWinner.SelectedItem as Member).ID;

                newResult.Tied = this.chkTie.Checked;
                newResult.SanctionedGame = this.chkSanctioned.Checked;

                this.membership.AddResult(newResult);
            }

            this.LoadResults();
        }

        private void DeleteResult_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to delete the selected results?", this.Text, MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                this.membership.RemoveGameResults((from DataGridViewRow row in this.gvResults.SelectedRows select (long)row.Cells["ID"].Value).ToList());

                this.LoadResults();
            }
        }

        private void ViewReports_Click(object sender, EventArgs e)
        {
            ReportsViewer rv = new ReportsViewer(this.membership);
            rv.ShowDialog();
        }

        private void SeedLevels_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Seeding Levels will destroy the existing rankings. This operation is not undoable. Do you want to continue?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                this.membership.SeedLevels();
            }
        }
    }
}
